package com.transcargo.request;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.StringTokenizer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.transcargo.action.ServletParametersException;
import com.transcargo.action.ServletValidationException;
import com.transcargo.db.db;
import com.transcargo.db.dbException;
import com.transcargo.geo.GeoObjectNotFoundException;
import com.transcargo.mail.MailHeader;
import com.transcargo.rpc.RPCOutput;
import com.transcargo.users.LongUser;
import com.transcargo.users.ShortUser;
import com.transcargo.users.User;
import com.transcargo.users.UserNotFoundException;
import com.transcargo.util.Normalizer;

public class GetMailHeadersRequest extends AuthRequest {
	private String xml = "";
	private Boolean inbox = true;
	private String in = "";
	
    private void servletGetValues(HttpServletRequest request) throws ServletParametersException {
    	in = request.getParameter("inbox");
    	if (in == null) in = "";
    }
    private void servletValidateValues() throws ServletValidationException {
    	if (!in.equals("on")) inbox = false; else inbox=true;
    }
    private void servletExecuteRequest() throws ServletRequestException {
    	MailHeader[] headers = null;
    	try {
    		if (inbox)
    			list = dbase.execute("SELECT id, sender_id, recipient_id, date, priority, subject, seen FROM mail WHERE recipient_id=" + User_Id + " AND owner=" + User_Id);
    		else
    			list = dbase.execute("SELECT id, sender_id, recipient_id, date, priority, subject, seen FROM mail WHERE sender_id=" + User_Id + " AND owner=" + User_Id);
    		iter = list.iterator();
    		headers = new MailHeader[list.size()];
    		int i = 0;
    		while (iter.hasNext()) {
    			String[] res = iter.next();
    			headers[i] = new MailHeader();
    			headers[i].id = Integer.parseInt(res[0]);
    			headers[i].sender = new User(Integer.parseInt(res[1]), dbase);
    			headers[i].recipient = new ShortUser(Integer.parseInt(res[2]), dbase);
    			headers[i].date = Normalizer.DenormalizeTime(res[3]);
    			headers[i].priority = res[4];
    			headers[i].subject = res[5];
    			headers[i].read = res[6].equals("1");
    			i++;
    		}
    		if (inbox)
    			xml =  RPCOutput.generate(headers,"folder","inbox");
    		else
    			xml =  RPCOutput.generate(headers,"folder","outbox");
    	} catch (GeoObjectNotFoundException e) {
    		throw new ServletRequestException();
    	} catch (UserNotFoundException e) {
    		throw new ServletRequestException();
		} catch (dbException e) {
    		throw new ServletRequestException();
		}
    }
    public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        PrintWriter out = null;
		try {
            out = response.getWriter();
            servletGetValues(request);
            servletValidateValues();
            servletExecuteRequest();
            out.println(xml);
	    } catch (ServletParametersException e) {
           sendRedirect(response, "mail.jsp");
        }  catch (ServletValidationException e) {
            sendError(response, new Exception(e.getMessage()));
        }  catch (ServletRequestException e) {
            String xml =  RPCOutput.generate(false);
            out.println(xml);
        }  catch (IOException e) {
            System.out.println("IOException: " + e.getMessage());
        }
    }
}
